package com.zhang.sdk.ui.dialog.bottom;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhang.sdk.R;

import java.util.List;

/*****************************************************
 * 版权信息: 2019 北京振中电子有限公司版权所有
 * 创建作者: zhang.x
 * 创建日期: 2020/5/18 13:46
 * ****************************************************
 * 更改记录: 更新人:    更新时间:    更新概要:
 *
 * ****************************************************
 *  类功能说明: 底部弹框样式
 * ****************************************************/
public class BottomMenuHelper {
    private Dialog mDialog;
    private View mView;
    private RvBottomMenuAdapter adapter;

    public static BottomMenuHelper newInstance() {
        return new BottomMenuHelper();
    }

    public void showBottomMenu(Activity context, List<BottomMenuItem> menuItems, OnMenuItemClickListener listener) {
        try{
            if (mDialog == null || adapter == null) {
                mView = View.inflate(context, R.layout.dialog_bottom_menu, null);
                RecyclerView rvMenu = mView.findViewById(R.id.rv_bottom_menu);
                adapter = new RvBottomMenuAdapter(context, menuItems);
                adapter.setOnItemClickListener(listener);
                rvMenu.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
                rvMenu.setAdapter(adapter);
                mDialog = new Dialog(context, R.style.loading_dialog_style);
                mDialog.setCancelable(true);
                Window window = mDialog.getWindow();
                if (window != null) {
                    window.setGravity(Gravity.BOTTOM);
                    window.setWindowAnimations(R.style.share_animation);
                }
                mDialog.setContentView(mView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            } else {
                adapter.setData(menuItems);
            }
            if (!mDialog.isShowing()) mDialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void hideBottomMenu() {
        if (mDialog!=null && mDialog.isShowing()) {
            mDialog.cancel();
        }
    }
}
